// server端需要
// npm config set strict-ssl false
// npm install mqtt
// 实现获取broker数据


function mqttutil() {
    var mqtt = require("mqtt");
    var client = mqtt.connect("mqtt://@orderer1.dc.dc");
    this.data = function () {
        client.on("connect", onConnectMessage);
        client.on("message", onMessageArrived);
        // client.subscribe(topic);
        console.log("test is ok");
    }
    function onConnectMessage(){
        console.log("connect success");
        client.subscribe("GATEWAY/data");
    }
    function onMessageArrived(message) {
        var id = client.getLastMessageId();
        console.log(id);
    }
}
module.exports = {
    mqttutil
}
// var mqtttest = new mqttutil();
// mqtttest.data("GATEWAY/data");







//封装成一个方法，暴露接口即可
// var hostname = "csxyz.xyz";      //主机IP  106.14.181.32    此域名已不是我在使用了(2019-6-16注)
// var hostPort = 9001;             //port
// var sub_count = 0;
// var client;
// function MQTTconnect() {
//     client = new Paho.MQTT.Client(hostname, hostPort, "test");
//     client.onMessageArrived = onMessageArrived;
//     client.connect({onSuccess:onConnect});
//     console.log("Host="+ hostname + ", port=" + hostPort);
// };
// // called when the client connects
// function onConnect() {
//   console.log("onConnect succeeds");
// //  client.subscribe('topic');
//   client.subscribe('#');    //订阅所有主题
// }
// // called when a message arrives
// function onMessageArrived(message) {
//     var topic = message.destinationName;
//     var payload = message.payloadString;
//     console.log("onMessageArrived:" + payload);
//     sub_count++;
//    $('#sub_count').val('收到订阅' + sub_count + '条');
//    $('#sub_message').prepend('<li>' + topic + ' = ' + payload + '</li>');
// }
// function button_onclick(){
//     var topic = $('#topicsend').val();
//     var text = $('#textsend').val();
//     if(topic=='' || text=='')
//     {
//         alert("noTopic or noText");
//         return;
//     }
//     var message = new Paho.MQTT.Message(text);
//     message.destinationName = topic;
//     message.qos=0;
//     client.send(message);
// };
// $(document).ready(function() {
//     MQTTconnect();
// });